import request from '../utils/request.js'
// import { getToken } from '@/utils/auth' // get token from cookie
const api = import.meta.env.VITE_API_ENV === 'development' ? '/api' : ''
const chatapi = import.meta.env.VITE_API_ENV === 'development' ? '/ai' : ''
const requesturl = `${api}/cfunc`
const loginurl = `${api}/login`
const downfileurl = '/download'
const upload = `${api}/upload`

// 通用方法
export function connrequest(CmdCode, data) {
  return request({
    url: requesturl,
    method: 'post',
    data: {
      ...data,
      Header:{
        CmdCode: CmdCode,
        EmployeeName: sessionStorage.getItem('Name')
        // EmployeeName: '姚电脑'
      },
      Token: sessionStorage.getItem('gptToken')
    }
  })
}

// 调用gpt聊天
export function getgptchat(data) {
  return request({
    url: `${chatapi}/ai/chat`,
    // url: requesturl,
    method: 'post',
    data: {
      ...data,
      Token: sessionStorage.getItem('gptToken')
    }
  })
}

// 调用gpt绘画
export function getgptimage(data) {
  return request({
    url: `${chatapi}/ai/image`,
    method: 'post',
    data: {
      ...data,
      Token: sessionStorage.getItem('gptToken')
    }
  })
}

// 登录
export function LoginEmployee(data) {
  return request({
    url: loginurl,
    method: 'post',
    data: {
      ...data,
      Header:{
        CmdCode: 'LoginEx',
      },
    }
  })
}
// 登录
export function login(data) {
  return request({
    url: loginurl,
    method: 'post',
    data: {
      ...data,
      Header:{
        CmdCode: 'LoginEmployee',
      },
    }
  })
}

// 下载
export function downloadfile(CmdCode, data) {
  return request({
    url: downfileurl,
    method: 'post',
    responseType:'arraybuffer',
    data: {
      ...data,
      Header:{
        CmdCode: CmdCode
      },
      // Token: getToken()
    }
  })
}

// 上传
export function uploadfile(data) {
  return request({
    url: upload,
    method: 'post',
    headers: { 
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': 'Bearer ' + sessionStorage.getItem('gptToken')
    },
    data
  })
}
export function uploadfile2(CmdCode, data) {
  return request({
    url: upload,
    method: 'post',
    data: {
      ...data,
      Header:{
        CmdCode: CmdCode
      },
      // Token: getToken()
    }
  })
}